package com.example.demostudent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

/**
 * Studeng Service
 *
 * @author   zfx(2515475790)
 * @create    2018年5月31日
 */
@Service
public class StudentService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Map<String,String> getList(String id,String regdate,String type,String group,String name,String code) {
        int count = 0;
        Student su =null;
        Map<String, String> map = new HashMap<String, String>();
        String sql_check = "select count(*) from jianyao WHERE id='"+id+"'";
        try {
            count = jdbcTemplate.queryForObject(sql_check, Integer.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        if(count==1){
            map.put("result", "1");
            map.put("code", "200");
            map.put("message", "增加失败");
        } else {
            String sql_insert = "INSERT INTO jianyao(id,regdate,type,groups,name,code) VALUES('" + id + "','" + regdate + "','" + type + "','"+group+"','"+name+"','"+code+"')";
            System.out.println(sql_insert);

            try {
                count = jdbcTemplate.update(sql_insert);
            } catch (DataAccessException e) {
                e.printStackTrace();
            }

            if (count == 1) {
                map.put("result", "0");
                map.put("code", "100");
                map.put("message", "增加成功");
                map.put("terminalcode", code);
            }
        }
        System.out.println(map);
        return map;
    }
    public Map<String,String> upDate(String id,String type,String group,String name,String date,String code) {
        int count = 0;
        Student su =null;
        Map<String, String> map = new HashMap<String, String>();
        String sql_check = "select count(*) from jianyao WHERE code='"+code+"'";
        try {
            count = jdbcTemplate.queryForObject(sql_check, Integer.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        if(count==1){
            count = 0;
            String sql_insert = "UPDATE jianyao SET type='"+type+"',groups='"+group+"',name='"+name+"',updatedate='"+date+"' WHERE id ='"+id+"'";
            System.out.println(sql_insert);

            try {
                count = jdbcTemplate.update(sql_insert);
            } catch (DataAccessException e) {
                e.printStackTrace();
            }
        }else{
            map.put("result", "1");
            map.put("code", "200");
            map.put("message", "密码不正确");
            map.put("terminalcode", code);
        }


            if (count == 1) {
                map.put("result", "0");
                map.put("code", "100");
                map.put("message", "修改成功");
                map.put("terminalcode", code);
            }

        System.out.println(map);
        return map;
    }
    public Map<String,String> concel(String date,String code) {
        int count = 0;
        Student su =null;
        Map<String, String> map = new HashMap<String, String>();
        String sql_check = "select count(*) from jianyao WHERE code='"+code+"'";
        try {
            count = jdbcTemplate.queryForObject(sql_check, Integer.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        if(count==1){
            count = 0;
            String sql_insert = "UPDATE jianyao SET conceldate='"+date+"',concelmark='1' WHERE code ='"+code+"'";
            System.out.println(sql_insert);

            try {
                count = jdbcTemplate.update(sql_insert);
            } catch (DataAccessException e) {
                e.printStackTrace();
            }
        }else{
            map.put("result", "1");
            map.put("code", "200");
            map.put("message", "密码不正确");
            map.put("terminalcode", code);
        }
        if (count == 1) {
            map.put("result", "0");
            map.put("code", "100");
            map.put("message", "删除成功");
            map.put("terminalcode", code);
        }

        System.out.println(map);
        return map;
    }
}